//
//  NewPersonalCenterHeaderView.m
//  suRead
//
//  Created by lidi on 2018/1/3.
//  Copyright © 2018年 bao. All rights reserved.
//

#import "NewPersonalCenterHeaderView.h"
#import "UIImageView+WebCache.h"
#import "AccountManager.h"

@interface NewPersonalCenterHeaderView()
@property (weak, nonatomic) IBOutlet UIImageView *imageViewIcon;
@property (weak, nonatomic) IBOutlet UILabel *labelName;
@property (weak, nonatomic) IBOutlet UILabel *labelScore;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewCover;

@property (weak, nonatomic) IBOutlet UIView *barContainer;

@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *headerItems;
@property (strong, nonatomic) NSDictionary *coverInfo;
@property (strong, nonatomic) NSNumber *userScore;
@end

@implementation NewPersonalCenterHeaderView

+ (instancetype)headerView {
    NewPersonalCenterHeaderView *headerView_ = [[NSBundle mainBundle] loadNibNamed:@"NewPersonalCenterHeaderView" owner:self options:nil][0];
    headerView_.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300.0f);
    headerView_.imageViewIcon.layer.cornerRadius = 35.0f;
    headerView_.imageViewIcon.layer.masksToBounds = YES;
    
    headerView_.barContainer.layer.cornerRadius = 10.0f;
    headerView_.barContainer.layer.masksToBounds = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:headerView_ action:@selector(iconClick:)];
    headerView_.imageViewIcon.userInteractionEnabled = YES;
    [headerView_.imageViewIcon addGestureRecognizer:tap];
    
    UITapGestureRecognizer *coverTap = [[UITapGestureRecognizer alloc] initWithTarget:headerView_ action:@selector(coverClick:)];
    headerView_.imageViewCover.userInteractionEnabled = YES;
    [headerView_.imageViewCover addGestureRecognizer:coverTap];
    
    for (UIView *item in headerView_.headerItems) {
        UITapGestureRecognizer *itemTap = [[UITapGestureRecognizer alloc] initWithTarget:headerView_ action:@selector(itemClick:)];
        item.userInteractionEnabled = YES;
        [item addGestureRecognizer:itemTap];
    }
    
    return headerView_;
}

- (void)refresh {
    if ([AccountManager sharedAccountManager].f_isLogined) {
        self.labelName.text = [AccountManager sharedAccountManager].f_nickname;
        [self.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:[AccountManager sharedAccountManager].f_user_img] placeholderImage:[UIImage imageNamed:@"icon_personal"]];
        if (!self.userScore) {
            self.userScore = @(0);
        }
        self.labelScore.text = [NSString stringWithFormat:@"积分: %@", self.userScore];
    } else {
        self.labelName.text = @"登录";
        self.labelScore.text = @"登录后更精彩";
        self.imageViewIcon.image = [UIImage imageNamed:@"icon_personal"];
    }
}

- (void)refreshCover:(NSDictionary *)info {
    self.coverInfo = info;
    NSString *coverUrl = [info objectForKey:@"settingBg"];
    if (coverUrl && [coverUrl isKindOfClass:[NSString class]] && [coverUrl length] > 0) {
        [self.imageViewCover sd_setImageWithURL:[NSURL URLWithString:coverUrl] placeholderImage:[UIImage imageNamed:@"user_bg"]];
    }
}

- (void)refreshScore:(NSNumber *)score {
    self.userScore = score;
    self.labelScore.text = [NSString stringWithFormat:@"积分: %@", score];
}

///头像点击
- (void)iconClick:(id)sender {
    if (self.iconTapHandle) {
        self.iconTapHandle();
    }
}

//封面点击
- (void)coverClick:(id)sender {
    NSString *url = [self.coverInfo objectForKey:@"bgpUrl"];
    if (url && [url isKindOfClass:[NSString class]] && [url length] > 0 && self.coverTapHandle) {
        self.coverTapHandle(url);
    }
}

- (void)itemClick:(id)sender {
    NSInteger index = [[sender view] tag];
    if (self.itemTapHandle) {
        self.itemTapHandle(index);
    }
}

@end
